package com.spa.application.service.admin;

import cn.hutool.core.util.ObjectUtil;
import com.spa.application.command.admin.TicketAddCommand;
import com.spa.application.command.admin.TicketEditCommand;
import com.spa.application.dto.PageResult;
import com.spa.application.dto.admin.TicketDTO;
import com.spa.application.dto.admin.TicketExportDTO;
import com.spa.application.dto.admin.TicketPickRecordDTO;
import com.spa.application.dto.admin.TicketPickRecordExportDTO;
import com.spa.application.query.admin.TicketPickRecordQuery;
import com.spa.application.query.admin.TicketQuery;
import com.spa.domain.entity.UserInfo;
import com.spa.domain.service.TicketService;
import com.spa.domain.service.UserInfoService;
import com.spa.domain.service.UserTicketService;
import com.spa.infrastructure.mapper.TicketMapperStruct;
import com.spa.infrastructure.mapper.UserTicketMapperStruct;
import com.spa.infrastructure.repository.condition.UserTicketRefCondition;
import com.spa.infrastructure.repository.service.UserTicketRefRepositoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class AdminTicketApplicationService {

    @Autowired
    private TicketService ticketService;

    @Resource
    private UserInfoService userInfoService;

    @Autowired
    private UserTicketRefRepositoryService userTicketRefRepositoryService;

    @Resource
    private UserTicketService userTicketService;


    public void addTicket(TicketAddCommand command) {
        ticketService.addTicket(command);
    }

    public void offline(TicketEditCommand command) {
        ticketService.offline(command);
    }

    public PageResult<TicketDTO> page(TicketQuery query) {
        return TicketMapperStruct.INSTANCE.entity2Dto4Page(ticketService.page(query));
    }

    public PageResult<TicketPickRecordDTO> pagePickRecord(TicketPickRecordQuery query) {
        UserTicketRefCondition condition = new UserTicketRefCondition();
        condition.setCurrent(query.getCurrent());
        condition.setSize(query.getSize());
        condition.setName(query.getName());
        PageResult<TicketPickRecordDTO> ticketPickRecordDTOPageResult = TicketMapperStruct.INSTANCE.po2Dto4Page(userTicketRefRepositoryService.page(condition));
        ticketPickRecordDTOPageResult.getRecords().forEach(dto -> {
            UserInfo userInfo = userInfoService.userInfoById(dto.getUserId());
            if (ObjectUtil.isNotNull(userInfo)) {
                dto.setUserName(userInfo.getName());
            }
        });
        return ticketPickRecordDTOPageResult;
    }

    public List<TicketExportDTO> list(TicketQuery query) {
        return TicketMapperStruct.INSTANCE.entity2ExportDTO(ticketService.list(query));
    }

    public List<TicketPickRecordExportDTO> receiveList(TicketPickRecordQuery query) {
        UserTicketRefCondition condition = new UserTicketRefCondition();
        condition.setName(query.getName());
        return UserTicketMapperStruct.INSTANCE.po2RecordDRO4List(userTicketService.list(condition));
    }
}
